from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import logging
import socket
import pymysql
logging.basicConfig(level=logging.INFO,format='%(asctime)s -%(levelname)s -%(process)d -%(module)s -%(funcName)s -%(message)s')
myname = socket.getfqdn(socket.gethostname(  ))
myaddr = socket.gethostbyname(myname)
print(myname)


print(myaddr)

db = pymysql.connect("192.168.2.247","xjzx","Af1121113","zthubo")
cursor = db.cursor()

cursor.execute("SELECT * FROM t_exam where IP='{0}' and status='0'".format(myaddr))
data = cursor.fetchone()
if (data==None):
    sql="SELECT * FROM t_exam where 考试日期='{0}' and status is null".format(time.strftime("%Y/%m/%d", time.localtime()))
    cursor.execute(sql)
    data = cursor.fetchone()
    if(data==None):
        print("无信息")
        a=input()
        exit()
    else:
        cursor.execute("UPDATE `t_exam` SET `status`='0', `IP`='{0}' WHERE `ID`='{1}'".format(myaddr,data[0]))
        db.commit()

user_names=data[1]
exam_codes=data[2]
phones=data[4]

browser = webdriver.Firefox()
url='http://colorcloudwebstudent.ynctjy.com/login.html'
browser.get(url)
def cl(obj):
    actions = ActionChains(browser)
    actions.move_to_element(obj)
    actions.click(obj)
    actions.perform()
def inphone():
    try:
        phone=browser.find_element_by_name('phone')
        phone.clear()
        phone.send_keys(phones.lstrip("\t"))
        
        mt_select=browser.find_element_by_id('mt_select')
        cl(mt_select)

        time.sleep(1)
        lis=browser.find_element_by_xpath("/html/body/div[1]/div/div/div[3]/div[2]/ul/li[2]")
        cl(lis)
        time.sleep(1)

        browser.execute_script("$('#update_submit').click()")
    except:
        logging.info("inphone1")           
    else:
        logging.info("inphone0")

def login():
    try:
        user_name=browser.find_element_by_id('user_name')
        exam_code=browser.find_element_by_id('exam_code')
        login_submit=browser.find_element_by_id('login_submit')
        user_name.clear()
        exam_code.clear()
        user_name.send_keys(user_names)
        exam_code.send_keys(exam_codes)
    
        cl(login_submit)

        


    except:
        logging.info("login")           
    else:
        logging.info("login")
while(True):
    if(browser.title=="同学您好！欢迎登录！"): 
        login()
    if(browser.title=="完善个人信息"):
        inphone()
    if(browser.title=="云南省第五届\"彩云杯\"中华优秀传统文化知识竞赛"):

        c=browser.execute_script("return $.getJSON('http://colorcloudwebstudent.ynctjy.com/student/info/getExamDate',function(data){return data});")
        if(c['data']['haveTest']==0):
            cursor.execute("UPDATE `t_exam` SET `status`='1', `IP`='{0}' WHERE `ID`='{1}'".format(myaddr,data[0]))
            db.commit()
            exit(0)
        else:
            time.sleep(15)
            logging.info("wait")
        #browser.execute_script("return b")
        try:
            
            c=browser.execute_script("$.getJSON('http://colorcloudwebstudent.ynctjy.com/student/info/getExamDate',function(data){b=data});return b['data']['havetest']")
            #b= browser.execute_script("return b;")
            #print(b)
            #print(c)
        except:
            print(0)
        else:
            print('test_complete')
    
    print(browser.title)

    
    time.sleep(3)